<?php

namespace App\Models\MainDB;

use Illuminate\Database\Eloquent\Model;

class GameSugarRecord extends Model
{
    protected $connection = 'mysql';

    protected $table = 'ln_game_sugar_record';

    public $timestamps = false;

    // 获取记录详情(包含用户信息)
    public function getRecordUserDetailByCondition($condition)
    {
        $field = "r.user_id,r.get_num,if(r.profit>0,1,0) as result,ud.nickname, ud.head_picture, ud.user_code";
        return $this->from($this->table . ' as r')
            ->selectRaw($field)
            ->where($condition)
            ->leftJoin('ln_user_data as ud', 'r.user_id', '=', 'ud.user_id')
            ->get()->toArray();
    }

    // 获取用户盈亏记录
    public function getAllUserProfitInfo($condition = [], $limit = 10)
    {
//        $field = "r.user_id,count(1) as join_num,sum(r.cost) as cost,sum(r.get_num) as get_num, sum(r.get_num * r.gift_price - r.cost) as total, ud.user_code,ud.nickname,ud.head_picture";
//        return $this->from($this->table . ' as r')
//            ->selectRaw($field)
//            ->where($condition)
//            ->leftJoin('ln_user_data as ud', 'r.user_id', '=', 'ud.user_id')
//            ->groupBy('r.user_id')
//            ->paginate($limit)
//            ->toArray();

        $field = "r.user_id,r.join_num,r.cost,r.sugar_num as get_num,r.profit as total, ud.user_code,ud.nickname,ud.head_picture";
        return $this->from('ln_game_sugar_user' . ' as r')
            ->selectRaw($field)
            ->where($condition)
            ->leftJoin('ln_user_data as ud', 'r.user_id', '=', 'ud.user_id')
            ->groupBy('r.user_id')
            ->paginate($limit)
            ->toArray();
    }
}
